﻿using System;
using System.Collections.Generic;
using System.IO;
using Windows.UI.Xaml.Media.Imaging;
using System.Linq;
using System.Windows.Input;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Devices.Input;
using Windows.UI.Core;
using System.Threading;
using System.Collections.Concurrent;

namespace RoboRun
{
    class CommandQueue
    {
        public static void addToCommandQueue(String command, TextBlock commandQueueTextBlock, ScrollViewer commandQueueScrollViewer) //add the command to the queue. i.e, add to the tail of the list. Could use array? Arraylist would be better.
        {
            GameplayScreen.commandQueue.Add(command);
            addToTextBox(command, commandQueueTextBlock, commandQueueScrollViewer);
        }

        public static void addToTextBox(String command, TextBlock commandQueueTextBlock, ScrollViewer commandQueueScrollViewer )
        {
            if (commandQueueTextBlock.Text.Length == 0)
            {
                commandQueueTextBlock.Text = command;
            }
            else
            {

                foreach (String i in GameplayScreen.commandQueue) //create one string out of everything in the arraylist to print in textblock 
                {
                    if (GameplayScreen.commandQueue.IndexOf(i, 0) == 0)
                    {
                        GameplayScreen.composite = i;
                    }
                    else
                    {
                        GameplayScreen.composite = (GameplayScreen.composite + "\n" + i);
                    }
                }
                commandQueueTextBlock.Text = GameplayScreen.composite;
                commandQueueScrollViewer.UpdateLayout();
                commandQueueScrollViewer.ScrollToVerticalOffset(commandQueueScrollViewer.ActualHeight); //auto scroll down
            }
        }

        public static void removeLastEntryFromCommandQueue(TextBlock commandQueueTextBlock)
        {
            if (GameplayScreen.commandQueue.Count != 0)
            {
                //this if condition works!
                if (GameplayScreen.commandQueue[GameplayScreen.commandQueue.Count - 1].Contains("["))
                {
                    if (GameplayScreen.commandQueue[GameplayScreen.commandQueue.Count - 2].Contains("For"))
                    {
                        GameplayScreen.formarker = false;
                        GameplayScreen.forButtonStatic.Source = new BitmapImage(new Uri("ms-appx:///Assets/GameplayScreen/ControlsButtons/forbutton.png", UriKind.Absolute
                      ));
                    }
                    else if (GameplayScreen.commandQueue[GameplayScreen.commandQueue.Count - 2].Contains("While"))
                    {
                        GameplayScreen.whilemarker = false;
                        GameplayScreen.whileButtonStatic.Source = new BitmapImage(new Uri("ms-appx:///Assets/GameplayScreen/ControlsButtons/whilebutton.png", UriKind.Absolute
                      ));
                    }
                    GameplayScreen.commandQueue.RemoveAt(GameplayScreen.commandQueue.Count - 1);
                    GameplayScreen.commandQueue.RemoveAt(GameplayScreen.commandQueue.Count - 1);
                    GameplayScreen.commandCounter -= 1;
                }
                //this works yay
                else if (GameplayScreen.commandQueue[GameplayScreen.commandQueue.Count - 1].Contains("]")) //for while and for loops
                {
                    int i = GameplayScreen.commandQueue.Count - 1;
               
                    for (int j = i; j >= 0; j--)
                    {
                        if (GameplayScreen.commandQueue[j].Contains("For"))
                        {
                          
                            GameplayScreen.formarker = true;
                            GameplayScreen.forButtonStatic.Source = new BitmapImage(new Uri("ms-appx:///Assets/GameplayScreen/ControlsButtons/finishbutton.png", UriKind.Absolute
                        ));
                            break;
                        }
                        else if (GameplayScreen.commandQueue[j].Contains("While"))
                        {
                         
                            GameplayScreen.whilemarker = true;
                            GameplayScreen.whileButtonStatic.Source = new BitmapImage(new Uri("ms-appx:///Assets/GameplayScreen/ControlsButtons/finishbutton.png", UriKind.Absolute
                        ));
                            break;
                        }
                       
                    }
                    GameplayScreen.commandQueue.RemoveAt(GameplayScreen.commandQueue.Count - 1);
                }
                else if (GameplayScreen.commandQueue[GameplayScreen.commandQueue.Count - 1].Contains("If"))
                {
                    GameplayScreen.ifButtonStatic.Source = new BitmapImage(new Uri("ms-appx:///Assets/GameplayScreen/ControlsButtons/ifbutton.png", UriKind.Absolute
                     ));
                    GameplayScreen.commandQueue.RemoveAt(GameplayScreen.commandQueue.Count - 1);
                    GameplayScreen.commandCounter -= 1;
                    GameplayScreen.elseMarker = false;
                    GameplayScreen.commandExecuted = "";
                }
                else if (GameplayScreen.commandQueue[GameplayScreen.commandQueue.Count - 1].Contains("Else"))
                {
                    GameplayScreen.ifButtonStatic.Source = new BitmapImage(new Uri("ms-appx:///Assets/GameplayScreen/ControlsButtons/elsebutton.png", UriKind.Absolute
                     ));
                    GameplayScreen.commandQueue.RemoveAt(GameplayScreen.commandQueue.Count - 1);
                    //GameplayScreen.commandCounter -= 1;
                    GameplayScreen.elseMarker = false;
                    GameplayScreen.commandExecuted = "If Statement";
                }

                else if (GameplayScreen.commandQueue[GameplayScreen.commandQueue.Count - 1].Contains("\t"))
                {
                    int i = GameplayScreen.commandQueue.Count - 1;
                    for (int j = i; j >= 0; j--)
                    {
                        if (GameplayScreen.commandQueue[j].Contains("Else"))
                        {
                            GameplayScreen.elseMarker = true;
                            GameplayScreen.commandExecuted = "If Statement";
                            GameplayScreen.ifButtonStatic.Source = new BitmapImage(new Uri("ms-appx:///Assets/GameplayScreen/ControlsButtons/finishbutton.png", UriKind.Absolute
                        ));
                            break;
                        }
                       
                    }
                    GameplayScreen.commandQueue.RemoveAt(GameplayScreen.commandQueue.Count - 1);
                }
                else if (GameplayScreen.commandQueue.Count > 2 && GameplayScreen.commandQueue[GameplayScreen.commandQueue.Count - 2].Contains("\t"))
                {
                   
                            int i = GameplayScreen.commandQueue.Count - 1;
                   
                            GameplayScreen.elseMarker = true;
                            GameplayScreen.commandExecuted = "If Statement";
                            GameplayScreen.ifButtonStatic.Source = new BitmapImage(new Uri("ms-appx:///Assets/GameplayScreen/ControlsButtons/finishbutton.png", UriKind.Absolute
                        ));
                    GameplayScreen.commandQueue.RemoveAt(GameplayScreen.commandQueue.Count - 1);
                    GameplayScreen.commandCounter -= 1;
      
                }


                else if (GameplayScreen.commandQueue[GameplayScreen.commandQueue.Count - 1].Contains("\t") || GameplayScreen.commandQueue[GameplayScreen.commandQueue.Count - 1].Contains("   "))
                {

                    GameplayScreen.commandQueue.RemoveAt(GameplayScreen.commandQueue.Count - 1);
                }

                //else if (GameplayScreen.commandQueue.Count > 2)
                //{
                //    //use when else is there
                //    if (GameplayScreen.commandQueue[GameplayScreen.commandQueue.Count - 2].Contains("\t"))
                //    {
                //        //RESUME CODING FROM HERE
                //        GameplayScreen.commandQueue.RemoveAt(GameplayScreen.commandQueue.Count - 1);
                //        GameplayScreen.commandCounter -= 1;
                //        GameplayScreen.elseMarker = true;
                //        GameplayScreen.commandExecuted = "If Statement";
                //    }
                //}
                else
                {
                    GameplayScreen.commandQueue.RemoveAt(GameplayScreen.commandQueue.Count - 1);
                    GameplayScreen.commandCounter -= 1;
                }

            }
        }

        public static void updateCommandQueueTextBlock(TextBlock commandQueueTextBlock, ScrollViewer commandQueueScrollViewer)
        {
            if (GameplayScreen.commandQueue.Count == 0)
            {
                GameplayScreen.composite = "";
            }
            else
            {
                foreach (String i in GameplayScreen.commandQueue) //create one string out of everything in the arraylist to print in textblock 
                {
                    if (GameplayScreen.commandQueue.IndexOf(i, 0) == 0)
                    {
                        GameplayScreen.composite = i;
                    }
                    else
                    {
                        GameplayScreen.composite = (GameplayScreen.composite + "\n" + i);
                    }
                }
            }
            commandQueueTextBlock.Text = GameplayScreen.composite;
            commandQueueScrollViewer.UpdateLayout();
            commandQueueScrollViewer.ScrollToVerticalOffset(commandQueueScrollViewer.ActualHeight); //auto scroll down
        }
    }
}
